<?php
/**
 * 工作流相关
 */
class workflow
{
    private $db;
    private $table;
    private $proctable;
    private $statustable;
    private $permtable;
    public  $error;

    public function __construct() {
        global $db;
        $this->db = &$db;
        $this->table = DB_PRE.'workflow';
        $this->proctable = DB_PRE.'workflow_process';
        $this->statustable = DB_PRE.'workflow_status';
        $this->permtable = DB_PRE.'perm';
    }

    /**
     * 检查数据唯一性
     * @author menghao
     * @param <string> $table
     * @param <string> $key
     * @param <string> $value
     * @return <boolean>
     */
    public function check_unique($table, $key, $value)
    {
        $this->db->prepare("select * from `$table` where status=? and $key=?");
        $this->db->set_int(STATUS_NORMAL);
        $this->db->set_string($value);
        $result = $this->db->execute();

        if(!empty($result)) return false;
        return true;
    }

    /**
     * 工作流方案列表
     * @author menghao
     * @return <array>
     */
    public function workflow_list()
    {
        return $this->db->query("select * from $this->table where status=".STATUS_NORMAL." order by id asc");
    }

    /**
     * 添加工作流方案
     * @author menghao
     * @param <array> $data
     * @return <boolean>
     */
    public function workflow_add($data)
    {
        global $response;
        if(!isset($data['name']) || empty($data['name']))
        {
            $this->error = "方案名称不能为空";
            $response->text_alert('error', 'name', $this->error);
        }
        elseif(!$this->check_unique($this->table, 'name', $data['name']))
        {
            $this->error = "方案名称已经存在";
            $response->text_alert('error', 'name', $this->error);
        }
        elseif(strlen($data['name'])>50)
        {
            $this->error = "方案名称不能超过50个字节";
            $response->text_alert('error', 'name', $this->error);
        }
        if(strlen($data['description'])>255)
        {
            $this->error = "方案描述不能超过255个字节";
            $response->text_alert('error', 'description', $this->error);
        }
        if(!empty($this->error))
        {
            return false;
        }
        $response->real_execute();
        
        $result = $this->db->insert($this->table, $data);
        return $result;
    }

    /**
     * 修改工作流方案
     * @author menghao
     * @param <int> $id
     * @param <array> $data
     * @return <boolean>
     */
    public function workflow_edit($id, $data)
    {
        global $response;
        $org_val = $this->workflow_get($id);

        if(!isset($data['name']) || empty($data['name']))
        {
            $this->error = "方案名称不能为空";
            $response->text_alert('error', 'name', $this->error);
        }
        elseif($org_val['name'] != $data['name'])
        {
            if(!$this->check_unique($this->table, 'name', $data['name']))
            {
                $this->error = "方案名称已经存在";
                $response->text_alert('error', 'name', $this->error);
            }
        }
        elseif(strlen($data['name'])>50)
        {
            $this->error = "方案名称不能超过50个字节";
            $response->text_alert('error', 'name', $this->error);
        }
        if(strlen($data['description'])>255)
        {
            $this->error = "方案描述不能超过255个字节";
            $response->text_alert('error', 'description', $this->error);
        }
        if(!empty($this->error))
        {
            return false;
        }
        $response->real_execute();
        
        $result = $this->db->simple_update($this->table, $data, array('id'=>$id));
        return $result;
    }

    /**
     * 获取一条工作流方案
     * @author menghao
     * @param <int> $id
     * @return <array>
     */
    public function workflow_get($id)
    {
        return $this->db->get_one($this->table, $id);
    }

    /**
     * 删除工作流方案
     * @author menghao
     * @param <int> $id
     * @return <boolean>
     */
    public function workflow_delete($id)
    {
        return $this->db->fake_delete($this->table, array('id'=>$id));
    }

    /**
     * 工作流步骤列表
     * @author menghao
     * @param <int> $workflowid
     * @return <type>
     */
    public function process_list($workflowid)
    {
        $this->db->prepare("select * from $this->proctable where status=? and workflowid=? order by id asc");
        $this->db->set_int(STATUS_NORMAL);
        $this->db->set_string($workflowid);
        $data = $this->db->execute();
        $result = array();
        if(is_array($data))
        {
            foreach($data as $r)
            {
                $result[$r['id']] = $r;
            }
        }
        return $result;
    }

    /**
     * 添加工作流步骤
     * @author menghao
     * @param <array> $data
     * @return <boolean>
     */
    public function process_add($data)
    {
        if(!$this->process_check($data)) return false;
        $this->db->insert($this->proctable, $data);
        $processid = $this->db->uuid();
        if(!empty($processid))
        {
            $this->set_perm($processid);
        }
        return $processid;
    }

    /**
     * 修改工作流步骤
     * @author menghao
     * @param <int> $id
     * @param <array> $data
     * @return <boolean>
     */
    public function process_edit($id, $data)
    {
        if(!$this->process_check($data)) return false;
        $result = $this->db->simple_update($this->proctable, $data, array('id'=>$id));
        if(!empty($result))
        {
            $this->set_perm($id);
        }
        return $result;
    }

    /**
     * 校验工作流步骤数据
     * @author menghao
     * @param <array> $data
     * @return <boolean>
     */
    private function process_check($data)
    {
        global $response;
        if(!isset($data['name']) || empty($data['name']))
        {
            $this->error = "步骤名称不能为空";
            $response->text_alert('error', 'name', $this->error);
        }
        elseif(strlen($data['name'])>50)
        {
            $this->error = "步骤名称不能超过50个字节";
            $response->text_alert('error', 'name', $this->error);
        }
        if(strlen($data['description'])>50)
        {
            $this->error = "步骤描述不能超过255个字节";
            $response->text_alert('error', 'description', $this->error);
        }
        if(!isset($data['passname']) || empty($data['passname']))
        {
            $this->error = "通过的操作名称不能为空";
            $response->text_alert('error', 'passname', $this->error);
        }
        elseif(strlen($data['passname'])>50)
        {
            $this->error = "通过的操作名称不能超过50个字节";
            $response->text_alert('error', 'passname', $this->error);
        }
        if(!isset($data['rejectname']) || empty($data['rejectname']))
        {
            $this->error = "退回的操作名称不能为空";
            $response->text_alert('error', 'rejectname', $this->error);
        }
        elseif(strlen($data['rejectname'])>50)
        {
            $this->error = "退回的操作名称不能超过50个字节";
            $response->text_alert('error', 'rejectname', $this->error);
        }
        if(empty($this->error))
        {
            return true;
        }
        $response->real_execute();
        return false;
    }


    /**
     * 获取一条步骤数据
     * @author menghao
     * @param <int> $id
     * @return <array>
     */
    public function process_get($id)
    {
        return $this->db->get_one($this->proctable, $id);
    }

    /**
     * 删除步骤
     * @author menghao
     * @param <int> $id
     * @return <type>
     */
    public function process_delete($id)
    {
        $result = $this->db->fake_delete($this->proctable, array('id'=>$id));
        if(!empty($result))
        {
            $this->delete_perm($id);
        }
        return $result;
    }

    /**
     * 内容状态列表
     * @author menghao
     * @return <array>
     */
    public function status_list()
    {
        return $this->db->query("select * from $this->statustable where status=".STATUS_NORMAL." order by value asc");
    }

    /**
     * 内容状态添加
     * @author menghao
     * @param <array> $data
     * @return <boolean>
     */
    public function status_add($data)
    {
        global $response;
        if(!isset($data['value']))
        {
            $this->error = "状态值不能为空";
            $response->text_alert('error', 'value', $this->error);
        }
        if(!isset($data['name']) || empty($data['name']))
        {
            $this->error = "状态名称不能为空";
            $response->text_alert('error', 'name', $this->error);
        }
        elseif(!$this->check_unique($this->statustable, 'name', $data['name']))
        {
            $this->error = "状态名称已经存在";
            $response->text_alert('error', 'name', $this->error);
        }
        elseif(strlen($data['name'])>50)
        {
            $this->error = "状态名称不能超过50个字节";
            $response->text_alert('error', 'name', $this->error);
        }
        if(strlen($data['description'])>255)
        {
            $this->error = "状态描述不能超过255个字节";
            $response->text_alert('error', 'description', $this->error);
        }
        if(!empty($this->error))
        {
            return false;
        }
        $response->real_execute();
        
        $result = $this->db->insert($this->statustable, $data);
        $this->status_cache();
        return $result;
    }

    /**
     * 内容状态修改
     * @author menghao
     * @param <int> $value
     * @param <array> $data
     * @return <boolean>
     */
    public function status_edit($value, $data)
    {
        global $response;
        if(!isset($data['name']) || empty($data['name']))
        {
            $this->error = "状态名称不能为空";
            $response->text_alert('error', 'name', $this->error);
        }
        elseif(strlen($data['name'])>50)
        {
            $this->error = "状态名称不能超过50个字节";
            $response->text_alert('error', 'name', $this->error);
        }
        if(strlen($data['description'])>255)
        {
            $this->error = "状态描述不能超过255个字节";
            $response->text_alert('error', 'description', $this->error);
        }
        if(!empty($this->error))
        {
            return false;
        }
        $response->real_execute();
        
        $result = $this->db->simple_update($this->statustable, $data, array('value'=>$value));
        $this->status_cache();
        return $result;
    }

    /**
     * 获取一条内容状态数据
     * @author menghao
     * @param <int> $value
     * @return <type>
     */
    public function status_get($value)
    {
        $value = intval($value);
        return $this->db->get_one($this->statustable, $value, 'value');
    }

    /**
     * 删除内容状态
     * @author menghao
     * @param <int> $value
     * @return <boolean>
     */
    public function status_delete($value)
    {
        $value = intval($value);
        $result = $this->db->fake_delete($this->statustable, array('value'=>$value));
        $this->status_cache();
        return $result;
    }

    /**
     * 缓存内容状态
     * @author menghao
     * @return <boolean>
     */
    public function status_cache()
    {
        $data = $this->status_list();
        $status = array();
        if(is_array($data))
        {
            foreach($data as $r)
            {
                $status[$r['value']] = $r['name'];
            }
        }
        cache_write('content_status', $status);
        return true;
    }

    public function get_status_by_id($id)
    {
        return $this->db->get_one($this->statustable, $id);
    }


    /**
     * 获取未被占用的状态编号
     * @author menghao
     * @return <array>
     */
    public function status_unused()
    {
        $used = cache_read('content_status');
        $used = empty($used) ? array() : $used;
        $unused = array();
        for($i=0; $i<100; $i++)
        {
            if(array_key_exists($i, $used)) continue;
            array_push(&$unused, $i);
        }
        return $unused;
    }

    /**
     * 添加权限元，如果存在则修改之
     * @author menghao
     * @param <int> $processid
     * @return <boolean>
     */
    private function set_perm($processid)
    {
        if(empty($processid)) return false;
        $process = $this->process_get($processid);
        $workflow = $this->workflow_get($process['workflowid']);
        $p_code = "process_$processid";
        $data = array(
            'p_name'  =>  "{$workflow['name']}({$process['name']})",
            'p_code'  =>  $p_code,
            'pt_code' =>  'content',
            'status'  =>  STATUS_NORMAL,
            'inputtime'=> TIME
        );
        $perm = $this->db->query("select * from $this->permtable where status=".STATUS_NORMAL." and p_code='$p_code'");
        if(!$perm)
        {
            $this->db->insert($this->permtable, $data);
        }
        else
        {
            $this->db->simple_update($this->permtable, $data, array('p_code'=>$p_code,'pt_code'=>'workflow'));
        }
        
        return true;
    }

    /**
     * 删除权限元
     * @author menghao
     * @param <int> $processid
     * @return <boolean>
     */
    private function delete_perm($processid)
    {
        return $this->db->fake_delete($this->permtable, array('p_code'=>"process_$processid",'pt_code'=>'content'));
    }

}
?>
